using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RapidHDL;

namespace MicroFabric
{
    public partial class FQuickFabric : Form
    {
        public ToplevelHardware oRapidHardware;

        public FQuickFabric()
        {
            InitializeComponent();
            oRapidHardware = new ToplevelHardware();
            if (!oRapidHardware.StartRapidHardware())
            {
                oRapidHardware.Transcript.AddTranscript("Hardware failed to ininitilize.");
                string sTranscript = oRapidHardware.Transcript.GetTranscript();
                System.Windows.Forms.MessageBox.Show(sTranscript, "Start Hardware Error", System.Windows.Forms.MessageBoxButtons.OK);
            }            
            tmrPipeline.Enabled = true;
            oRapidHardware.LoadRAM();
            oRapidHardware.SystemInterface.SystemControl.StartClock();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int iA = Conversion.AnyStringToInt(textBox1.Text);
            int iB = Conversion.AnyStringToInt(textBox2.Text);

            FPGABlock oBlock = new FPGABlock();
            oBlock.AddEntry(0, 0, (uint)iA, true);
            oBlock.AddEntry(0, 0, (uint)iB, true);

            IOInterface.PostFPGA(oBlock);
        }

        private void cmdSum_Click(object sender, EventArgs e)
        {
            int iA = Conversion.AnyStringToInt(textBox1.Text);
            int iB = Conversion.AnyStringToInt(textBox2.Text);

            FPGABlock oBlock = new FPGABlock();
            oBlock.AddEntry(0, 0, (uint)iA, true);
            oBlock.AddEntry(0, 0, (uint)iB, true);

            IOInterface.PostFPGA(oBlock);
        }

        private void tmrPipeline_Tick(object sender, EventArgs e)
        {
            IOInterface.CheckFPGA();
            if (IOInterface.PacketsIn.PacketCount > 0)
            {
                foreach (Packet oPacket in IOInterface.PacketsIn.Packets)
                {
                    if (oPacket.Data != 0)
                        lblResult2.Text = oPacket.Data.ToString();
                }
                IOInterface.PacketsIn.Reset();
            }
        }

        private void FQuickFabric_Load(object sender, EventArgs e)
        {  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            oRapidHardware.LaunchGUI(false);
        }

    }
}